package com.example.mypic.models;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.content.Context;

import com.example.mypic.utils.Utils;

public class PicHandler extends DefaultHandler{
	private List<IPWallpaperImage> items;
	private IPWallpaperImage item;
	private StringBuilder sBuilder;

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		sBuilder = new StringBuilder();
		items = new ArrayList<IPWallpaperImage>();
		super.startDocument();
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if(sBuilder != null){
			sBuilder.append(ch, start, length);
		}
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		sBuilder = new StringBuilder();
		if(localName.equalsIgnoreCase(Utils.IPWALLPAPER_IMAGE)){
			item = new IPWallpaperImage();
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		if(localName.equalsIgnoreCase(Utils.IPWALLPAPER_IMAGE)){
			items.add(item);
		}else if(localName.equalsIgnoreCase(Utils.CATEGORYID)){
			item.setCategoryID(Integer.parseInt(sBuilder.toString()));
		}else if(localName.equalsIgnoreCase(Utils.IMAGEID)){
			item.setImageID(Integer.parseInt(sBuilder.toString()));
		}else if(localName.equalsIgnoreCase(Utils.IMAGE)){
			item.setImage(sBuilder.toString());
		}else if(localName.equalsIgnoreCase(Utils.THUMNAIL_IMAGE)){
			item.setThumnaiImage(sBuilder.toString());
		}else if(localName.equalsIgnoreCase(Utils.VIEW)){
			item.setView(Integer.parseInt(sBuilder.toString()));
		}else if(localName.equalsIgnoreCase(Utils.DOWNLOAD)){
			item.setDownload(Integer.parseInt(sBuilder.toString()));
		}else if(localName.equalsIgnoreCase(Utils.DEVICE)){
			item.setDevice(sBuilder.toString());
		}else if(localName.equalsIgnoreCase(Utils.WIDTH)){
			item.setWidth(Integer.parseInt(sBuilder.toString()));
		}else if(localName.equalsIgnoreCase(Utils.HEIGHT)){
			item.setHeight(Integer.parseInt(sBuilder.toString()));
		}else if(localName.equalsIgnoreCase(Utils.FAVORITED)){
			item.setFavorited(Integer.parseInt(sBuilder.toString()));
		}
	}
	
	public List<IPWallpaperImage> getItems(){
		return this.items;
	}
}
